package com.supor.aiot.websocket;

import android.content.Context;
import android.text.TextUtils;

/**
 * ---------------------------------------------------------------- <br>
 * Copyright (C) 2014-2021, by het, ShenZhen, All rights reserved.  <br>
 * ---------------------------------------------------------------- <br>
 * <p>
 * 描述: WsManager <br>
 * 作者: lei <br>
 * 日期: 2021/9/20 <br>
 */
public class WsManager {
    private volatile static WsManager instance;

    private WsImpl wsImpl;
    private OnReceiveMessageListener onReceiveMessageListener;

    private WsManager() {
        wsImpl = new WsImpl();
    }

    public static WsManager getInstance() {
        if (instance == null) {
            synchronized (WsManager.class) {
                if (instance == null) {
                    instance = new WsManager();
                }
            }
        }
        return instance;
    }

    /**
     * 初始化
     *
     * @param context
     * @param url
     */
    public void init(Context context, String url) {
        if (wsImpl == null) {
            return;
        }
        WsImpl.init(context.getApplicationContext(), url);
    }


    /**
     * 链接服务器
     *
     * @param callback
     */
    public void connect(ConnectCallback callback) {
        if (wsImpl == null) {
            return;
        }
        WsImpl.connect(callback);
    }


    /**
     * 设置接受消息的回掉
     *
     * @param listener li
     */
    public void setOnReceiveMessageListener(final OnReceiveMessageListener listener) {
        onReceiveMessageListener = listener;
        onReceived();
    }

    /**
     * 设置消息解析之后的回调
     */
    private void onReceived() {
        if (wsImpl == null) {
            return;
        }

        WsImpl.setOnReceiveMessage(msg -> {
            if (onReceiveMessageListener != null) {
                onReceiveMessageListener.onReceived(msg);
            }
        });
    }

    /**
     * 退出
     */
    public void logout() {
        if (wsImpl == null) {
            return;
        }
        WsImpl.disConnectionSocket();
    }


    /**
     * 重连websocket
     */
    public void reConnectionWebsocket() throws Exception {
        if (wsImpl == null) {
            return;
        }
        WsImpl.reConnectionWebsocket();
    }

    /**
     * client 是否为空
     */
    public boolean isNullReconnectionWebsocket() {
        if (wsImpl == null) {
            return true;
        }
        return WsImpl.isNullReconnectionWebsocket();
    }

    /**
     * 判断client是否为关闭状态
     */
    public boolean isCloseConnectionWebsocket() {
        if (wsImpl == null) {
            return true;
        }
        return WsImpl.isCloseConnectionWebsocket();
    }

    /**
     * 发送消息
     *
     * @param message
     */
    public void sendMessage(String message) {
        if (TextUtils.isEmpty(message)) {
            Logc.e("websocket", "Message sending cannot null ");
            return;
        }
        WsImpl.sendMessage(message);
    }

    /**
     * 发送心跳包
     */
    public void sendHeartPackage(String heartBeat) {
        if (wsImpl == null) {
            return;
        }
        WsImpl.sendMessage(heartBeat);
    }
}
